package com.huarenkeji.china_push.core;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import android.os.Build;
import com.hihonor.push.sdk.HonorPushClient;
import com.hihonor.push.sdk.HonorPushCallback;
import com.huarenkeji.china_push.PushManager;
import com.huarenkeji.china_push.util.Logger;

import java.util.HashMap;
import java.util.Map;

public class HonorPushInterface {

    public static final String MANUFACTURER = "HONOR";
    public static final String BUNDLE_APP_ID = "HONOR_APP_ID";

    public void init(Context context) {
        ///荣耀不需要初始化
        boolean isSupport = HonorPushClient.getInstance().checkSupportHonorPush(context);
        if (isSupport) {
            HonorPushClient.getInstance().init(context, true);
            Logger.i("init Honor Push Called.");
        } else {
            String errorMsg = "init Honor Push fail not support";
            Logger.e(errorMsg);
            PushManager.onRegisterFail(errorMsg);
        }
    }

    public void register(Context context, Bundle bundle) {
        // 获取PushToken
        HonorPushClient.getInstance().getPushToken(new HonorPushCallback<String>() {
            @Override
            public void onSuccess(String pushToken) {
                Logger.i("getPushToken pushToken:" + pushToken);
                PushManager.onRegisterSuccess(pushToken, HonorPushInterface.MANUFACTURER);
                //获取到token之后开启消息推送功能
                requestNotificationPermission();
            }

            @Override
            public void onFailure(int errorCode, String errorString) {
                Logger.e(errorCode+":"+errorString);
            }
        });
    }

    /**
     * 开启通知权限弹窗
     */
    public void requestNotificationPermission() {
        //打开通知栏消息状态
        HonorPushClient.getInstance().turnOnNotificationCenter(new HonorPushCallback<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Logger.i("turnOnNotificationCenter onSuccess:" );
            }

            @Override
            public void onFailure(int errorCode, String errorString) {
                Logger.e(errorCode+":"+errorString);
            }
        });
    }

    /**
     * 打开通知栏权限设置
     */
    public void openNotificationSetting(Context context) {
        try {
            Intent intent = new Intent();
            if (Build.VERSION.SDK_INT >= 26) {
                // android 8.0引导
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
            } else {
                // android 5.0-7.0
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);
            }
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {

        }
    }

    public void processIntent(@NonNull Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Map<String, Object> pushContetMap = new HashMap<>();
            for (String key : bundle.keySet()) {
                String content = bundle.getString(key);
                pushContetMap.put(key, content);
                Logger.i("receive data from push, key = " + key + ", content = " + content);
            }
            PushManager.onOpenNotification(pushContetMap);
        } else {
            Logger.e("HonorPushInterface processIntent fail bundle is Null");
        }
    }

    public boolean supportPush(Context context, Bundle bundle) {
        String appId = bundle.getString(BUNDLE_APP_ID);
        return !TextUtils.isEmpty(appId) && HonorPushClient.getInstance().checkSupportHonorPush(context);
    }

    public String getManufacturer() {
        return MANUFACTURER;
    }


}
